/**
 * 
 */
package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsalgorithm.spec.parse;
import com.tc.dispatch.gsservice.basic.common.regulation;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.OrderSumMain;
import com.tc.dispatch.gsservice.model.OrderSumSub;
import com.tc.dispatch.gsservice.model.ProcessStep;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.model.combinedOrder;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.FloatJsonValueProcessor;
import com.tc.dispatch.web.util.tools;

/**
 * @author cajeep
 *
 */
public class orderJieSuan extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orderJieSuan() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();
		if (action.equals("list")) {
	
			String id=request.getParameter("id");
			List ret = new ArrayList();
			if(id==null||id.equals(""))
				out.print("{success:false, data:'false'}");
			try{
				ret=gss.findOrderSumMain("where id="+id);
			}
			catch(GSException e)
			{
				System.out.println(e.getMessage());
				out.print("{success:false, data:'false'}");				
			}
			JSONObject json = new JSONObject();
			JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor());
			cfg.registerJsonValueProcessor(java.sql.Date.class,
					new DateJsonValueProcessor());
			cfg.registerJsonValueProcessor(java.lang.Float.class, new FloatJsonValueProcessor());

			// BeanUtils.copyProperties();
			json.put("totalProperty", new Integer(1));
			

			JSONArray arr = JSONArray.fromObject(ret, cfg);
			json.put("osmRoot", arr);

			response.getWriter().print(json);
		} else if (action.equals("sort")) {
			try {
				List sortList = gss.findProcessStep("");
				Iterator it = sortList.iterator();
				ProcessStep ps;
				List ret = new ArrayList();
				List retList = new ArrayList();

				while (it.hasNext()) {
					ps = (ProcessStep) it.next();
					if (ret.indexOf(ps.getSpecName()) < 0) {
						ret.add(ps.getSpecName());
						retList.add(ps);
					} else {
						// sortList.remove(ps);
					}
				}
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(ret.size()));

				JSONArray arr = JSONArray.fromObject(retList.toArray(), cfg);
				json.put("root", arr);

				response.getWriter().print(json);
			} catch (GSException e) {
				out.print("{success:false, data:'false'}");
			}

		} else if (action.equals("scheme")) {
			try {
				String sort = request.getParameter("sortType");
				List sortList = gss.findProcessStep(" where specName='" + sort
						+ "'");
				Iterator it = sortList.iterator();
				ProcessStep ps;
				List ret = new ArrayList();
				List retList = new ArrayList();
				while (it.hasNext()) {
					ps = (ProcessStep) it.next();
					if (ret.indexOf(new Integer(ps.getScheme())) < 0) {
						ret.add(new Integer(ps.getScheme()));
						retList.add(ps);
					}
				}
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				// json.put("totalProperty", new Integer(ret.size()));

				JSONArray arr = JSONArray.fromObject(retList.toArray(), cfg);
				json.put("root", arr);
				// json.

				response.getWriter().print(json);
			} catch (GSException e) {
				out.print("{success:false, data:'false'}");
			}
		} else if (action.equals("step")) {
			try {
				String sort = request.getParameter("sortType");
				String scheme = request.getParameter("scheme");
				List sortList = gss.findProcessStep(" where specName='" + sort
						+ "' and scheme=" + scheme);
				List ret = sortList;
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(ret.size()));

				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);

				response.getWriter().print(json);
			} catch (GSException e) {
				out.print("{success:false, data:'false'}");
			}
		}

		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
